#include "solver.h"

pwork *setup(params *p)
{
  idxint i = 0;
  double *ptr;
  static double b[35] = {0.0, 0.0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5, -0.5, 0.0, 0.0, 0.0, -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  b[2] = p->B;
  static double c[48] = {-1}; /* rest = {0.0}; */
  static double h[31]; /* = {0.0}; */
  static idxint q[4] = {3, 3, 4, 11};
  static idxint Gjc[49] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31};
  static idxint Gir[31] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 13, 14, 15, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
  static double Gpr[31] = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
  static idxint Ajc[49] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 14, 15, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 32, 39, 46, 53, 60, 67, 74, 81, 88, 95, 102, 113, 115, 117, 119, 122, 125};
  static idxint Air[125] = {14, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 0, 1, 0, 2, 1, 3, 15, 20, 21, 22, 25, 1, 4, 15, 20, 21, 22, 26, 1, 5, 15, 20, 21, 22, 27, 1, 6, 15, 20, 21, 22, 28, 1, 7, 15, 20, 21, 22, 29, 1, 8, 15, 20, 21, 22, 30, 1, 9, 15, 20, 21, 22, 31, 1, 10, 15, 20, 21, 22, 32, 1, 11, 15, 20, 21, 22, 33, 1, 12, 15, 20, 21, 22, 34, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 16, 16, 17, 17, 18, 19, 17, 23, 24};
  static double Apr[125] = {-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,1.0,1.0,0,0,0,0,0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,-1.0,0,-1.0,1.0,0.5,-0.5,1.0,0.5,-0.5};
  static const idxint F_ind_map[30] = {35, 36, 37, 42, 43, 44, 49, 50, 51, 56, 57, 58, 63, 64, 65, 70, 71, 72, 77, 78, 79, 84, 85, 86, 91, 92, 93, 98, 99, 100};
  ptr = (double *) p->F;
  for (i = 0; i < 30; ++i) {
    Apr[ F_ind_map[i] ] = *ptr++;
  }
  static const idxint d_ind_map[10] = {38, 45, 52, 59, 66, 73, 80, 87, 94, 101};
  ptr = (double *) p->d;
  for (i = 0; i < 10; ++i) {
    Apr[ d_ind_map[i] ] = *ptr++;
  }
  Apr[117] = p->gamma;
  static const idxint mu_ind_map[10] = {34, 41, 48, 55, 62, 69, 76, 83, 90, 97};
  ptr = (double *) p->mu;
  for (i = 0; i < 10; ++i) {
    Apr[ mu_ind_map[i] ] = *ptr++;
  }
  return ECOS_setup(48 /* num vars */, 31 /* num cone constraints */, 35 /* num eq constraints */, 10 /* num linear cones */, 4 /* num second-order cones */, q, Gpr, Gjc, Gir, Apr, Ajc, Air, c, h ,b);
}

int solve(pwork *w, vars *sol)
{
  idxint i = 0;
  int exitflag = ECOS_solve(w);
  for(i = 0; i < 10; ++i) {
    sol->x[i] = w->x[i + 32];
  }
  return exitflag;
}

void cleanup(pwork *w)
{
  ECOS_cleanup(w,0);
}

